package com.zyit.dao;

import com.zyit.DataObject.T_user;
import com.zyit.util.*;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserDao {
    private DBHelp dbHelp = new DBHelp();


    public T_user findUserNameAndPassword(String name, String password) {
        String sql = "select id,account,nick_name,password,gender,id_card_no,email,phone,type,open_id from t_user where account=? && password=?";
        return dbHelp.executeQueryForObject(sql,new BeanRowMapper<T_user>(T_user.class),name,password);
    }

    public T_user emailByUser(String email){
        String sql = "select * from t_user where email=?";
        return dbHelp.executeQueryForObject(sql,new BeanRowMapper<T_user>(T_user.class),email);
    }

    public T_user nameByUser(String name) {
        String sql = "select *from t_user where account=?";
        return dbHelp.executeQueryForObject(sql,new BeanRowMapper<T_user>(T_user.class),name);
    }
    public Integer UpdataPassword(String account,String email,String password){
        String sql = "update t_user set password=? where account =? and email=?";
        return dbHelp.executeUpdate(sql,password,account,email);
    }

    public Page<T_user> findByPage(Integer pageNo, Integer pageSize) throws SQLException {
        Integer count = count();
        //Page<Book> page = new Page<>(pageNo,count);
        Page<T_user> page = new Page<>(pageNo, count, pageSize);
        String sql = "select id,bookname bookName,author from book limit ?,?";
        List<T_user> T_userList = dbHelp.executeQueryForList(sql, new BeanRowMapper<>(T_user.class), page.getStart(), page.getPageSize());
        page.setPageList(T_userList);

        return page;

    }

    public  Page<T_user> findByPage(Integer pageNo) throws SQLException {
        Integer count = count();
        Page<T_user> page = new Page<>(pageNo, count);
        String sql = "select * from t_user limit ?,?";
        List<T_user> bookList = dbHelp.executeQueryForList(sql, new BeanRowMapper<>(T_user.class), page.getStart(), page.getPageSize());
        page.setPageList(bookList);

        return page;

    }


    public Integer count() throws SQLException {
        String sql = "select count(*) from t_user";
        Map<String, Object> map = dbHelp.executeQueryForObject(sql, new MapRowMapper());
        Long count = (Long) map.get("count(*)");
        return count.intValue();
    }
}
